package com.ttc.ssm.service.home.impl;

import com.ttc.ssm.common.JobPage;
import com.ttc.ssm.common.SearchJobPage;
import com.ttc.ssm.mapper.admin.*;
import com.ttc.ssm.mapper.home.*;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.home.HomeJobService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Hui on 2016/8/9.
 */
public class HomeJobServiceImpl implements HomeJobService{
    @Autowired
    JobPropertyMapper jobPropertyMapper;
    @Autowired
    IndustryMapper industryMapper;
    @Autowired
    HomeJobTypeMapper homeJobTypeMapper;
    @Autowired
    DegreeMapper degreeMapper;
    @Autowired
    SalaryTypeMapper salaryTypeMapper;
    @Autowired
    JobTagMapper jobTagMapper;
    @Autowired
    ZoneMapper zoneMapper;
    @Autowired
    CityMapper cityMapper;
    @Autowired
    JobMapper jobMapper;
    @Autowired
    TagToJobMapper tagToJobMapper;

    @Autowired
    HomeJobMapper homeJobMapper;

    @Autowired
    HomeCityMapper homeCityMapper;

    @Autowired
    HomeIndustryMapper homeIndustryMapper;

    @Autowired
    HomeZoneMapper homeZoneMapper;

    @Autowired
    HomeDeliveryMapper homeDeliveryMapper;

    @Autowired
    HomeInviteMapper homeInviteMapper;

    @Autowired
    HomeDeliveryActivityMapper homeDeliveryActivityMapper;

    @Override
    public List<JobProperty> getJobProperty() throws Exception {
        return jobPropertyMapper.getJobProperty();
    }

    @Override
    public List<Industry> getIndustry() throws Exception {
        return industryMapper.getIndustry();
    }

    @Override
    public List<JobType> getJobTypeByIndustryId(int industry_id) throws Exception {
        return homeJobTypeMapper.getJobTypeByIndustryId(industry_id);
    }

    @Override
    public List<Degree> getDegree() throws Exception {
        return degreeMapper.getDegree();
    }

    @Override
    public List<SalaryType> getSalaryType() throws Exception {
        return salaryTypeMapper.getSalaryType();
    }

    @Override
    public List<JobTag> getJobTab(int company_id) throws Exception {
        return jobTagMapper.getJobTag(company_id);
    }

    @Override
    public List<Zone> getZone() throws Exception {
        return zoneMapper.getZone();
    }

    @Override
    public List<City> getCityByZoneId(int zone_id) throws Exception {
        return cityMapper.getCityByZoneId(zone_id);
    }

    @Override
    public int insertJob(int company_id,Job job) throws Exception {
        Date date = new Date();
        job.setDate_added(date);
        job.setDate_modified(date);
        job.setRead(0);
        job.setOrder(0);
        job.setCompany_id(company_id);
        jobMapper.insertJob(job);
        Map map = new HashMap();
        map.put("job_id",job.getJob_id());
        map.put("tag",job.getTag());
        if(job.getTag()!=null)
            tagToJobMapper.insertTag(map);
        return 0;
    }

    @Override
    public int countRows(JobPage jobPage) throws Exception {
        return homeJobMapper.countRows(jobPage);
    }

    @Override
    public Job selectJobById(Integer job_id) throws Exception {
        return homeJobMapper.selectJobById(job_id);
    }

    @Override
    public List<Job> selectJobForPage(JobPage jobPage) throws Exception {
        return homeJobMapper.selectJobForPage(jobPage);
    }

    @Override
    public Job selectJobById(int jobId) throws Exception {
        return jobMapper.selectJobById(jobId);
    }

    @Override
    public JobType getJobTypeByJobTypeId(int job_type_id) throws Exception {
        return homeJobTypeMapper.getJobTypeByJobTypeId(job_type_id);
    }

    @Override
    public String getIndustryNameById(int industry_id) throws Exception {
        return homeIndustryMapper.getIndustryNameById(industry_id);
    }

    @Override
    public int getZoneByCityId(int city_id) throws Exception{
        return homeCityMapper.getZoneByCityId(city_id);
    }

    @Override
    public String getZoneNameById(int zone_id) throws Exception {
        return homeZoneMapper.getZoneNameById(zone_id);
    }

    @Override
    public List<TagToJob> getTagForJob(int job_id) throws Exception {
        return tagToJobMapper.getTagForJob(job_id);
    }

    @Override
    public List<Job> selectJob(SearchJobPage searchJobPage) throws Exception {
        return homeJobMapper.selectJob(searchJobPage);
    }

    @Override
    public int countRowsForSearchJob(SearchJobPage searchJobPage) throws Exception {
        return homeJobMapper.countRowsForSearchJob(searchJobPage);
    }

    @Override
    public int selectCompanyIdByJobId(int job_id) throws Exception {
        return homeJobMapper.selectCompanyIdByJobId(job_id);
    }

    @Override
    public void insertDelivery(Delivery delivery) throws Exception {
        delivery.setDate_added(new Date());
        delivery.setStatus(0);
        homeDeliveryMapper.insertDelivery(delivery);

        DeliveryActivity deliveryActivity = new DeliveryActivity();
        deliveryActivity.setDelivery_id(delivery.getDelivery_id());
        deliveryActivity.setCustomer_id(delivery.getCustomer_id());
        deliveryActivity.setCompany_id(delivery.getCompany_id());
        deliveryActivity.setStatus(0);
        deliveryActivity.setType(1);
        deliveryActivity.setIs_read(0);
        deliveryActivity.setDate_added(new Date());
        homeDeliveryActivityMapper.insertActivity(deliveryActivity);
    }

    @Override
    public int countDeliveryRowsByJobId(int job_id) throws Exception {
        return homeDeliveryMapper.countRowsByJobId(job_id);
    }

    @Override
    public int countInviteRowsByJobId(int job_id) throws Exception {
        return homeInviteMapper.countRowsByJobId(job_id);
    }

    @Override
    public int checkIsSend(int resume_id, int job_id) throws Exception {
        Map map = new HashMap();
        map.put("resume_id", resume_id);
        map.put("job_id", job_id);
        return homeDeliveryMapper.checkIsSend(map);
    }


}
